class UserDropdown {
    constructor() {
        this.container = document.getElementById('userArea');
        this.menu = this.container.querySelector('.user-menu');
        this.timer = null;

        this.initEventListeners();
        this.loadUserInfo();
    }

    initEventListeners() {
        // 悬浮逻辑
        this.container.addEventListener('mouseenter', () => this.showMenu());
        this.container.addEventListener('mouseleave', () => this.hideMenu(300));

        this.menu.addEventListener('mouseenter', () => this.showMenu());
        this.menu.addEventListener('mouseleave', () => this.hideMenu(200));

    }

    showMenu() {
        clearTimeout(this.timer);
        this.menu.classList.add('active');
    }

    hideMenu(delay) {
        this.timer = setTimeout(() => {
            this.menu.classList.remove('active');
        }, delay);
    }

    async loadUserInfo() {
        try {
            const data = await fetchData(API.USER_CENTER.USER_INFO);
            document.getElementById('headerUserName').textContent = data.userName;
            document.getElementById('headerAvatar').src = data.avatar || '/images/avatars/default-logo.png';
        } catch (e) {
            Alert.show('用户信息加载失败', 'error');
        }
    }

    async handleLogout() {
        const confirm = await Alert.confirm('确定要退出登录吗？');
        if (confirm) {
            try {
                await fetchResponse(API.AUTH.LOGOUT);
                window.location.href = '/user/login';
            } catch (e) {
                Alert.show(e.message || '退出失败', 'error');
            }
        }
    }
}